/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Servlets;

import beans.CardCube;
import beans.Categoria;
import beans.ConsumptionPattern;
import beans.Controlador;
import beans.CustomerZipcode;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Harold Jara
 */
public class Consumo extends HttpServlet {

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException, Exception {
        response.setContentType("text/html;charset=UTF-8");
        
        
        PrintWriter out = response.getWriter();
        try {
            
            if(request.getParameter("btnCategorias")!= null){
                List<Categoria> listaCategorias = Controlador.GetInstance().sendGetCategories("xml");

                out.println("<!DOCTYPE html>");
                out.println("<html>");
                out.println("<head>");
                out.println("<title>Servlet Consumo</title>");            
                out.println("</head>");
                out.println("<body>");
                out.println("<h1>Consumo Categorías</h1>");

                out.print("<table border='1'>");
                out.print("<thead>");
                out.print("<th>Code</th>");
                out.print("<th>Description</th>");
                out.print("<thead>");
                out.print("<tbody>");
                for(Categoria item : listaCategorias){
                    out.print("<tr>");
                    out.print("<td>"+item.getCode()+"</td>");
                    out.print("<td>"+item.getDescription()+"</td>");
                    out.print("</tr>");
                }
                out.print("</tbody>");
                out.print("</table");

                out.println("</body>");
                out.println("</html>");
            }
            else if(request.getParameter("btnCustomZipCodes")!= null){
                HashMap<String, List<CustomerZipcode>> hmZipcdes =  Controlador.GetInstance().sendGetCustomerZipcodes("xml", "201211", "201303", "month", "es_auto", "28028", 0, 0, 0, 0, "payments");
                
                out.println("<!DOCTYPE html>");
                out.println("<html>");
                out.println("<head>");
                out.println("<title>Servlet Consumo</title>");            
                out.println("</head>");
                out.println("<body>");
                out.println("<h1>Consumo CustomerZipCodes</h1>");
                out.print("<br/>");
                
                for (Map.Entry<String, List<CustomerZipcode>> elemento : hmZipcdes.entrySet()) {
                    out.print("<h3>Mes: "+elemento.getKey()+"</h3>");
                    out.print("<table border='1'>");
                    out.print("<thead>");
                    out.print("<th>Zipcode</th>");
                    out.print("<th>Cards</th>");
                    out.print("<th>Payments</th>");
                    out.print("<th>Incomes</th>");
                    out.print("</thead>");
                    out.print("<tbody>");
                    List<CustomerZipcode> listaZipCodes = elemento.getValue();
                    for(CustomerZipcode item : listaZipCodes){
                        out.print("<tr>");
                        out.print("<td>"+item.getZipcode()+"</td>");
                        out.print("<td>"+item.getNumCards()+"</td>");
                        out.print("<td>"+item.getNumPayments()+"</td>");
                        out.print("<td>"+item.getIncomes()+"</td>");
                        out.print("</tr>");
                    }
                    out.print("</tbody>");
                    out.print("</table");
                    out.print("<br/>");
                }
                
                
                out.println("</body>");
                out.println("</html>");
            }
            else if(request.getParameter("btnCardsCube") != null){
                
                 HashMap<String, List<CardCube>> hmCardCubes =  Controlador.GetInstance().sendGetCardsCube("xml", "201211", "201303", "month", "es_auto", "28028", 0, 0, 2, 1);
                
                out.println("<!DOCTYPE html>");
                out.println("<html>");
                out.println("<head>");
                out.println("<title>Servlet Consumo</title>");            
                out.println("</head>");
                out.println("<body>");
                out.println("<h1>Consumo CustomerZipCodes</h1>");
                out.print("<br/>");
                
                for (Map.Entry<String, List<CardCube>> elemento : hmCardCubes.entrySet()) {
                    out.print("<h3>Mes: "+elemento.getKey()+"</h3>");
                    out.print("<table border='1'>");
                    out.print("<thead>");
                    out.print("<th>Genero</th>");
                    out.print("<th>GrupoEdad</th>");
                    out.print("<th>Cards</th>");
                    out.print("<th>Payments</th>");
                    out.print("<th>AVG</th>");
                    out.print("</thead>");
                    out.print("<tbody>");
                    List<CardCube> listaCardCubes = elemento.getValue();
                    for(CardCube item : listaCardCubes){
                        out.print("<tr>");
                        out.print("<td>"+item.getGenero()+"</td>");
                        out.print("<td>"+item.getGrupoEdad()+"</td>");
                        out.print("<td>"+item.getNroCards()+"</td>");
                        out.print("<td>"+item.getNroPayments()+"</td>");
                        out.print("<td>"+item.getPromedioGasto()+"</td>");
                        out.print("</tr>");
                    }
                    out.print("</tbody>");
                    out.print("</table");
                    out.print("<br/>");
                }
                
                
                out.println("</body>");
                out.println("</html>");
            }
            else if(request.getParameter("btnConsumePattern") != null){
                
                HashMap<String, List<ConsumptionPattern>> hmConsumptionPatterns =  Controlador.GetInstance().sendGetConsumptionPattern("xml", "201211", "201303", "month", "es_auto", "28028", 0, 0, 0, 0);
                
                out.println("<!DOCTYPE html>");
                out.println("<html>");
                out.println("<head>");
                out.println("<title>Servlet Consumo</title>");            
                out.println("</head>");
                out.println("<body>");
                out.println("<h1>Consumo CustomerZipCodes</h1>");
                out.print("<br/>");
                
                for (Map.Entry<String, List<ConsumptionPattern>> elemento : hmConsumptionPatterns.entrySet()) {
                    out.print("<h3>Mes: "+elemento.getKey()+"</h3>");
                    out.print("<table border='1'>");
                    out.print("<thead>");
                    out.print("<th>Day</th>");
                    out.print("<th>AVG</th>");
                    out.print("<th>MAX</th>");
                    out.print("<th>MIN</th>");
                    out.print("<th>STD</th>");
                    out.print("<th>MODE</th>");
                    out.print("<th>CARDS</th>");
                    out.print("<th>PAYMENTS</th>");
                    out.print("</thead>");
                    out.print("<tbody>");
                    List<ConsumptionPattern> listaConsumptionPatterns = elemento.getValue();
                    for(ConsumptionPattern item : listaConsumptionPatterns){
                        out.print("<tr>");
                        out.print("<td>"+item.getRangoTiempo()+"</td>");
                        out.print("<td>"+item.getPromedio()+"</td>");
                        out.print("<td>"+item.getMaximo()+"</td>");
                        out.print("<td>"+item.getMinimo()+"</td>");
                        out.print("<td>"+item.getEstandar()+"</td>");
                        out.print("<td>"+item.getModo()+"</td>");
                        out.print("<td>"+item.getNroCards()+"</td>");
                        out.print("<td>"+item.getNroPayments()+"</td>");
                        out.print("</tr>");
                    }
                    out.print("</tbody>");
                    out.print("</table");
                    out.print("<br/>");
                }
                
                
                out.println("</body>");
                out.println("</html>");
            }
        } finally {            
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (Exception ex) {
            Logger.getLogger(Consumo.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (Exception ex) {
            Logger.getLogger(Consumo.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
